#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time   :2025/7/1310:12
@Author : zengjiahao1989@gmail.com
@File   :router.py
"""
from dataclasses import dataclass

from flask import Flask, Blueprint
from injector import inject

from internal.handler import AppHandler


@inject
@dataclass
class Router:
    """路由"""

    app_handler: AppHandler

    # 依赖注入需要用构造函数生成
    # def __init__(self, app_handler: AppHandler):
    #     self.app_handler = app_handler

    # 如果依赖的路由有多个需要用dataclass就可以自动生成,就不用多次生成构造函数

    def register_router(self, app: Flask):
        """注册路由"""
        # 1.创建一个蓝图
        bp = Blueprint("llmops", __name__, url_prefix="")

        # 2.将url对应的控制器方法做绑定
        bp.add_url_rule("/ping", methods=["GET", "POST", "DEL"], view_func=self.app_handler.ping)
        bp.add_url_rule("/apps/<uuid:app_id>/debug", methods=["POST"], view_func=self.app_handler.debug)
        bp.add_url_rule("/app", methods=["POST"], view_func=self.app_handler.create_app)
        bp.add_url_rule("/app/<uuid:id>", methods=["GET"], view_func=self.app_handler.get_app)
        bp.add_url_rule("/app/<uuid:id>", methods=["POST"], view_func=self.app_handler.update_app)
        bp.add_url_rule("/app/<uuid:id>/delete", methods=["DELETE"], view_func=self.app_handler.delete_app)
        # 3.在应用上注册蓝图
        app.register_blueprint(bp)
